package com.dream.utils.hibernate;

import java.sql.SQLException;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.dream.utils.log.DreamLogUtils;
import com.dream.utils.system.cfg.SystemConfigUtils;

/**
 * @copyright evan
 * @author evan
 * @Revision
 * @date 2012/9/22
 */
@SuppressWarnings("rawtypes")
public class PagingCriteriaSearchCallback<T> implements HibernateCallback<T> {
	
	private DetachedCriteria criteria;
	
	private String pageIndex = "";

	private String pageSize = "";
	private static final int DEF_SIZE = SystemConfigUtils.instance().getInteger("system.pagesize.default", 10);
	
	public PagingCriteriaSearchCallback(){
		
	}
	
	public PagingCriteriaSearchCallback(DetachedCriteria criteria){
		
		this.criteria=criteria;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public T doInHibernate(Session session) throws HibernateException, SQLException {

		return (T)doPaging(criteria.getExecutableCriteria(session), pageIndex, pageSize).list();
	}

	private  Criteria doPaging(Criteria criteria, String pageIndex, String pageSize){
		
		int index = 1;
		int size = DEF_SIZE;
		try{
			size = Integer.parseInt(pageSize);
		}catch(Exception e){
			String info = new StringBuilder(this.getClass().getName())
					.append("pageSize=").append(pageSize).toString();
			DreamLogUtils.error(this, "doPaging", info, e);
			size = DEF_SIZE;
		}
		try{
			index = Integer.parseInt(pageIndex);
		}catch(Exception e){
			String info = new StringBuilder(this.getClass().getName()).append("pageIndex=")
					.append(pageIndex).toString();
			DreamLogUtils.error(this, "doPaging", info, e);
			index = 1;
		}
			
		if(index < 1){
			index = 1;
		}
		
		if(size < 1){
			size = DEF_SIZE;
		}
		
		criteria.setMaxResults(size);
		criteria.setFirstResult((index-1) * Integer.valueOf(size));
		return criteria;
	}

	public PagingCriteriaSearchCallback setCriteria(DetachedCriteria criteria) {
		this.criteria = criteria;
		return this;
	}

	public PagingCriteriaSearchCallback setPageIndex(String pageIndex) {
		this.pageIndex = pageIndex;
		return this;
	}

	public PagingCriteriaSearchCallback setPageSize(String pageSize) {
		this.pageSize = pageSize;
		return this;
	}
}
